/**
 * 广告支出-按渠道（内容商）统计
 */   
$(function(){
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}
	$(".pageBox button").css("background-color","#478df1");
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
				
			}else{
				if(page==1){
					var channel = $("#channel").val();
					byTotalChannel(page,channel);
				}else if(page>showPageN-4){
					
				}else{
					byTotalChannel0(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
})
function byTotalChannel0(page){
	var startTime;
	var endTime;
	var channel = $("#channel").val();
		$.ajax({
			url : '/web-adv/byTotalChannel',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"channel":channel},
			type : 'post',
			success : function(data){
				$('#adContent').find("tr").eq(0).siblings("tr").remove();
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byTotalChannel2(page);	
		            }
				});
			},
	        error: function(){}        
	});
}
//页面加载事件
function byTotalChannel(page,adName){
	if(page==null || page.length<=0){
		var page=1;
	}
	var startTime;
	var endTime;
	var channel = $("#channel").val();
		$.ajax({
			url : '/web-adv/byTotalChannel',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"channel":channel},
			type : 'post',
			success : function(data){
				$('#adContent').find("tr").eq(0).siblings("tr").remove();
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byTotalChannel2(page);	
		            }
				});
			var MyPays=0;	
			var beans = data.beans;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			var clickNums=0;
			//循环输出表格
			for(var i = 0; i < beans.length;i++){
				var tbody = "<tr><td>"+(i+1)+"</td>" +
				"<td>"+beans[i].channel +"</td>" +
				"<td>"+beans[i].chNumber+"</td>" +
				"<td>"+beans[i].adCount+"</td>" +
				"<td>"+beans[i].chProperty+"</td>" +
				"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
				"<td>"+beans[i].clickNum+"</td>" +
				"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
				"<td>"+beans[i].adBudget+"</td>" +
				"<td>"+((page-1)*5+(i+1))+"</td>"+
				"<td><a class='btn btn-primary' href='/web-adv/advWeb/views/adTotalPay/totalByAdContentOneday.html?selectedNav=3&navPos=2&endTime="+data.endTime+"&startTime="+data.startTime+"&channel="+beans[i].channel+"&chNumber="+beans[i].chNumber+"'>查看</a></td>"+"</tr>"
				
				$('#adContent').append(tbody);
				
			}
			var allBean=data.allBean;
			for (var i = 0; i < allBean.length;i++) {
				MyPays+=allBean[i].adPay*1;
				var proportion=(allBean[i].proportion*100).toFixed(2);
				proportions+=proportion*1;
				adBudgets+=allBean[i].adBudget*1;
				clickNums+=allBean[i].clickNum*1;
			}
			var tbodyTwo="<tr>" +
			"<td>总计</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>"+clickNums+"</td>" +
			"<td>"+MyPays.toFixed(2)+"</td>" +
		    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
		    "<td>"+adBudgets.toFixed(2)+"</td>" +
		    "<td>---</td>" + 
		    "<td>---</td>" + 
		 "</tr>";
			if(data.pages==data.pageNum){
				$('#adContent').append(tbodyTwo);
			}
		},
        error: function(){}        
	});
}

//为了分页做的函数
function byTotalChannel2(page){
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
		$.ajax({
			url : '/web-adv/byTotalChannel',
	        data:{"startTime":startTime,"endTime":endTime,"page":page},
			type : 'post',
			success : function(data) {
				$('#adContent').find("tr").eq(0).siblings("tr").remove();
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				var MyPays=0;	
				var beans = data.beans;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var clickNums=0;
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					
					var tbody = "<tr><td>"+(i+1)+"</td>" +
					"<td>"+beans[i].channel +"</td>" +
					"<td>"+beans[i].chNumber+"</td>" +
					"<td>"+beans[i].adCount+"</td>" +
					"<td>"+beans[i].chProperty+"</td>" +
					"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
					"<td>"+beans[i].clickNum+"</td>" +
					"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
					"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
					"<td>"+beans[i].adBudget+"</td>" +
					"<td>"+((page-1)*5+(i+1))+"</td>"+
					"<td><a class='btn btn-primary' href='/web-adv/advWeb/views/adTotalPay/totalByAdContentOneday.html?selectedNav=3&navPos=2&endTime="+data.endTime+"&startTime="+data.startTime+"&channel="+beans[i].channel+"'>查看</a></td>"+"</tr>"
					
					
					$('#adContent').append(tbody);
				}
				var allBean=data.allBean;
				for (var i = 0; i < allBean.length;i++) {
					MyPays+=allBean[i].adPay*1;
					var proportion=(allBean[i].proportion*100).toFixed(2);
					proportions+=proportion*1;
					adBudgets+=allBean[i].adBudget*1;
					clickNums+=allBean[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" + 
			    "<td>---</td>" + 
			 "</tr>";
				if(data.pages==data.pageNum){
					$('#adContent').append(tbodyTwo);
				}
			},
	        error: function(){}        
		});
  }

//查询按钮
function byAdTotalChannel(startTime,endTime,channel){	
	$('#adContent tr td').remove();
	if(page==null || page.length<=0){
		var page=1;
	}
	$.ajax({
		url : '/web-adv/byTotalChannel',
        data:{"startTime":startTime,"endTime":endTime,"page":page,"channel":channel},
		type : 'post',
		success : function(data){
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	byTotalChannel2(page);	
	            }
			});
			var MyPays=0;	
			var beans = data.beans;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			var clickNums=0;
		//循环输出表格
		for(var i = 0; i < beans.length;i++){
			
			var tbody = "<tr><td>"+(i+1)+"</td>" +
			"<td>"+beans[i].channel +"</td>" +
			"<td>"+beans[i].chNumber+"</td>" +
			"<td>"+beans[i].adCount+"</td>" +
			"<td>"+beans[i].chProperty+"</td>" +
			"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
			"<td>"+beans[i].clickNum+"</td>" +
			"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
			"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
			"<td>"+beans[i].adBudget+"</td>" +
			"<td>"+((page-1)*5+(i+1))+"</td>"+
			"<td><a class='btn btn-primary' href='/web-adv/advWeb/views/adTotalPay/totalByAdContentOneday.html?selectedNav=3&navPos=2&endTime="+data.endTime+"&startTime="+data.startTime+"&channel="+beans[i].channel+"'>查看</a></td>"+"</tr>"
			
			$('#adContent').append(tbody);
		}
		var allBean=data.allBean;
		for (var i = 0; i < allBean.length;i++) {
			MyPays+=allBean[i].adPay*1;
			var proportion=(allBean[i].proportion*100).toFixed(2);
			proportions+=proportion*1;
			adBudgets+=allBean[i].adBudget*1;
			clickNums+=allBean[i].clickNum*1;
		}
		var tbodyTwo="<tr>" +
		"<td>总计</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>---</td>" +
		"<td>"+clickNums+"</td>" +
		"<td>"+MyPays.toFixed(2)+"</td>" +
	    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
	    "<td>"+adBudgets.toFixed(2)+"</td>" +
	    "<td>---</td>" + 
	    "<td>---</td>" + 
	 "</tr>";
			
			if(data.pages==data.pageNum){
				$('#adContent').append(tbodyTwo);
			}
	},
    error: function(){}        
});
}